﻿using System.Net;
using System.Net.Http;

namespace Neo4jConnect.Rest
{
    public class HttpClientNodeService : INodeService
    {
        private readonly HttpClient client;

        public HttpClientNodeService(HttpClient client)
        {
            this.client = client;
        }
        
        public string GetRoot()
        {
            return client
                .Get("")
                .EnsureStatusCode(HttpStatusCode.OK)
                .Content.ReadAsString();                  
        }

        public string Create()
        {
            return Create(string.Empty);
        }

        public string Create(string nodeData)
        {
            return client
                .Post("node", new StringContent(nodeData))
                .EnsureStatusCode(HttpStatusCode.Created)
                .Content.ReadAsString();            
        }

        public string Get(ulong nodeId)
        {
            return client
                .Get(string.Format("node/{0}", nodeId))
                .EnsureStatusCode(HttpStatusCode.OK)
                .Content.ReadAsString();                        
        }

        public void SetProperties(ulong nodeId, string nodeData)
        {            
            client
                .Put(string.Format("node/{0}/properties", nodeId), new StringContent(nodeData))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }

        public string GetProperties(ulong nodeId)
        {            
            return client
                .Get(string.Format("node/{0}/properties", nodeId))
                .EnsureStatusCode(HttpStatusCode.OK, HttpStatusCode.NoContent)
                .Content.ReadAsString();           
        }

        public void RemoveProperties(ulong nodeId)
        {
            client
                .Delete(string.Format("node/{0}/properties", nodeId))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }

        public void SetProperty(ulong nodeId, string propertyName, string propertyValue)
        {
            var requestUri = string.Format("node/{0}/properties/{1}", nodeId, propertyName);

            client
                .Put(requestUri, new StringContent(@"""" + propertyValue + @""""))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }

        public string GetProperty(ulong nodeId, string propertyName)
        {            
            return client
                .Get(string.Format("node/{0}/properties/{1}", nodeId, propertyName))            
                .EnsureStatusCode(HttpStatusCode.OK)
                .Content.ReadAsString();            
        }

        public void RemoveProperty(ulong nodeId, string propertyName)
        {            
            client
                .Delete(string.Format("node/{0}/properties/{1}", nodeId, propertyName))
                .EnsureStatusCode(HttpStatusCode.NoContent);            
        }

        public void Delete(ulong nodeId)
        {
            if (nodeId > 0)
            {
                client
                    .Delete(string.Format("node/{0}", nodeId))
                    .EnsureStatusCode(HttpStatusCode.NoContent);
            }
        }
    }
}